// pages/payOline/payOline.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    totalPrice: 0,
    user: '',
    order: [],
    storeName: '',
    foods: '',
    storeImage: '',
    foodId:[],
    user_adress:''
  },
  // 确认支付
  confirmPay() {
    wx.showLoading({
      title: '审核中',
      success: (res) => {
        setTimeout((res) => {
          wx.hideLoading({
            success: (res) => {
              wx.showToast({
                title: '支付成功',
                duration: 1500,
                icon: 'success'
              })
              
              let storeName = this.data.storeName
              this.yunGetStoreImgPath(storeName)
            }
          })
        }, 1000)
      }
    })
  },
  // 取消支付
  toBack(){
    wx.navigateBack({
      delta: 2,
      success: (res) => {
        wx.showToast({
          title: '订单取消！',
          duration: 800,
          icon: 'error'
        })
      }
    })
  },
  yunGetStoreImgPath(store) {
    let that = this
    let db = wx.cloud.database()
    db.collection('CR_stores')
      .where({
        storeName: store
      })
      .get({
        success: (res) => {
          that.setData({
            storeImage: res.data[0].imagePath
          })
          // 获得当前时间
          let date = new Date()
          let year = date.getFullYear()
          let month = date.getMonth() + 1
          let day = date.getDate()
          let h = date.getHours()
          let m = date.getMinutes()
          let nowdate = year + '.' + month + '.' + day + '  ' + h + ':' + m
          // 设置订单数据
          let totalPrice = this.data.totalPrice
          let detail = this.data.foods
          let user = this.data.user
          let storeName = this.data.storeName
          var storeImage = this.data.storeImage
          let foodId = this.data.foodId
          let adress = this.data.user_adress
          this.yunAddOrder('CR_orders', storeName, totalPrice, detail, user, nowdate, storeImage,foodId,adress)
        }
      })
  },
  // 添加记录到订单表
  yunAddOrder(table, storeName, totalPrice, order, user, date, storeImage,foodId,adress) {
    let db = wx.cloud.database()
    let that = this
    db.collection(table)
      .add({
        data: {
          store: storeName,
          totalPrice: totalPrice,
          detail: order,
          user: user,
          state: '待确认',
          date: date,
          comment: '0',
          storeImage: storeImage,
          foodId:foodId,
          adress:adress
        },
        success: (res) => {
          wx.navigateBack({
            delta: 2
          })
        }
      })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    this.setData({
      totalPrice: options.totalPrice,
      order: wx.getStorageSync('order'),
      storeName: options.storeName,
      user: wx.getStorageSync('Tel'),
      user_adress:options.adress
    })
    let arr = this.data.order
    let foodid = []
    for (let i = 0; i < arr.length; i++) {
      foodid.push(arr[i]._id)
      if (i == 0) {
        that.setData({
          foods: arr[i].foodName + 'x' + arr[i].num,
          foodId:foodid
        })
      } else {
        let foods = this.data.foods
        that.setData({
          foods: foods + ',' + arr[i].foodName + 'x' + arr[i].num,
          foodId:foodid
        })
      }
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})